package main.java.indi.zyj.c150.dp;

import java.util.Scanner;

public class _03_NumberTriangles {


    static int[][] triangles = new int[1001][1001];
    static int[][] temp = new int[1001][1001];


    public static void main(String[] args) {

        Scanner scan = new Scanner(System.in);

        int r = scan.nextInt();
        for (int i = 1; i <= r; i++) {
            for (int j = 1; j <= i; j++) {
                triangles[i][j] = scan.nextInt();
            }
        }

        temp[1][1] = triangles[1][1];

        for (int i = 2; i <= r; i++) {
            for (int j = 1; j <= i; j++) {
                temp[i][j] = Math.max(temp[i - 1][j], temp[i - 1][j - 1]) + triangles[i][j];
            }
        }

        //求最终答案
        int max = -0x3f;
        for(int i = 1; i <= r; i++)
        {
            if(temp[r][i] > max){
                max = temp[r][i];
            }

        }
        for (int i = 1; i <= r; i++) {

            for (int j = 1; j <= i; j++) {
                System.out.print(temp[i][j]);
            }
            System.out.println();
        }

        System.out.println(max);

    }


}
